<template>
  <!-- 表单验证
      基本表单验证：
        1: el-form   
            model绑定相应表单数据
            rules:绑定相应的规则
            ref:用于获取el-form的实例对象
        2：el-form-item
            prop:绑定需要验证的字段名
        3：
        rules：{
          prop的值：[
          {required:true,message:"错误信息",trigger:"blur/change"},
          {min:最小字符长度,max:最大字符长度,message:"错误信息",trigger:"blur/change"},
          {
            validator:(rule,value,callback)=>{
              rule:规则
              value:当前验证项的值
              callback:
                 如果验证成功  callback()
                 如果验证不成功  callback(new Error(错误信息))

            }，
            trigger:"blur/change"
          }
          ]
        }
          全局表单验证
          1：el-form 上加入ref
          2:
            this.$refs.form.validate(result=>{
              result:true表示验证通过
              false:验证失败
            })    

       -->
  <!-- 组件根标签 -->
  <div class="login">
    <div class="main">
      <el-form class="form" ref="form" :model="form" :rules="rules">
        <img class="img" src="./../../assets/common/login-logo.png" alt="" />
        <el-form-item prop="mobile">
          <el-input
            prefix-icon="el-icon-user"
            v-model="form.mobile"
            @keyup.enter.native="submit"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            prefix-icon="el-icon-unlock"
            v-model="form.password"
            type="password"
            @keyup.enter.native="submit"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button
            class="btn"
            style="backgroundColor:#407ffe;width:100%"
            type="primary"
            @click="submit"
            >登录</el-button
          >
        </el-form-item>
        <el-form-item>
          账号：13800000002 密码：123456
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script>
export default {
  name: 'login',
  data () {
    return {
      form: {
        mobile: '13800000002',
        password: '123456'
      },
      rules: {
        mobile: [
          { required: true, message: '手机号必须填写哦', trigger: 'blur' },
          {
            validator: (rule, value, callback) => {
              const _reg = /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/
              if (_reg.test(value)) {
                callback()
              } else {
                callback(new Error('请输入正确格式的手机号码'))
              }
            },
            trigger: 'blur'
          }
        ],
        password: [
          { required: true, message: '密码必须填写哦', trigger: 'blur' },
          { min: 6, max: 16, message: '请输入6-16位密码', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 全局表单验证提交
    submit () {
      this.$refs.form.validate(async result => {
        if (result) {
          // this.$message.success('表单验证成功')
          // 调用vuex中的子模块user中的异步方法
          await this.$store.dispatch('user/getUserToken', this.form)
          // 跳转到首页
          this.$message.success('登录成功')
          this.$router.push('/')
        } else {
          this.$message.error('表单验证失败，辣鸡')
        }
      })
    }
  },
  components: {}
}
</script>
<style lang="scss" scoped>
.login {
  height: 100%;
  background: url('~@/assets/common/login.jpg');
  display: flex;
  justify-content: center;
  align-items: center;
  .form {
    width: 500px;
    text-align: center;
    color: #fff;
    .img {
      padding-bottom: 15px;
    }
    .btn {
      border: none;
    }
  }
}
</style>
